package com.wing.wcabinet.controller.app;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.response.CellSingleInventoryProductVO;
import com.wing.common.utils.JsonResult;
import com.wing.wcabinet.model.entity.WcabCellSingleInventoryProduct;
import com.wing.wcabinet.model.response.WcabCellSingleInventoryProductVO;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.wcabinet.model.entity.WcabUseInventoryProduct;
import com.wing.wcabinet.model.response.WcabUseInventoryProductVO;
import com.wing.wcabinet.service.WcabUseInventoryProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.wcabinet.model.request.WcabUseInventoryProductForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-产品在用库存管理", tags = "产品在用库存管理")
@RestController("appWcabUseInventoryProductController")
@RequestMapping("wcabinet/app/wcabUseInventoryProduct")
public class WcabUseInventoryProductController {

    @Autowired
    private WcabUseInventoryProductService wcabUseInventoryProductService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "查询货道上产品库存(领用列表)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "productCategoryId", value = "产品分类id", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "productTypeId", value = "产品类型id", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "skuName", value = "SKU名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "brandName", value = "品牌名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "typeName", value = "产品类型名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCodes", value = "柜子编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/return/list")
    public PageResult<WcabUseInventoryProductVO> list(
            @RequestParam(name = "page",  defaultValue = "1", required = false) Integer page
            , @RequestParam(name = "limit",  defaultValue = "10", required = false) Integer limit
            , @RequestParam(name = "sort",  defaultValue = "create_time", required = false) String sort
            , @RequestParam(name = "order",  defaultValue = "asc", required = false) String order
            , Long productCategoryId
            , Long productTypeId
            , String cabCodes
            , String skuName
            , String brandName
            , String typeName
            , String name
            , Long workOrderId
    ) {
        Page<WcabUseInventoryProductVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("productCategoryId",productCategoryId);
        params.put("productTypeId",productTypeId);
        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("skuName",skuName);
        params.put("brandName",brandName);
        params.put("typeName",typeName);
        params.put("name",name);
        params.put("workOrderId",workOrderId);
        params.put("queryFlag","return");

        Page<WcabUseInventoryProductVO> list = wcabUseInventoryProductService.pageListSku4app(pg, params);
        PageResult pageResult=new PageResult<>(list.getRecords(), list.getTotal());
        return pageResult;
    }


    @ApiOperation(value = "查询货道上产品库存(领用列表)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "productCategoryId", value = "产品分类id", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "productTypeId", value = "产品类型id", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "skuName", value = "SKU名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "brandName", value = "品牌名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "typeName", value = "产品类型名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCodes", value = "柜子编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/repair/list")
    public PageResult<WcabUseInventoryProductVO> listRepair(
            @RequestParam(name = "page",  defaultValue = "1", required = false) Integer page
            , @RequestParam(name = "limit",  defaultValue = "10", required = false) Integer limit
            , @RequestParam(name = "sort",  defaultValue = "create_time", required = false) String sort
            , @RequestParam(name = "order",  defaultValue = "asc", required = false) String order
            , Long productCategoryId
            , Long productTypeId
            , String cabCodes
            , String skuName
            , String brandName
            , String typeName
            , String name
            , Long workOrderId
    ) {
        Page<WcabUseInventoryProductVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("productCategoryId",productCategoryId);
        params.put("productTypeId",productTypeId);
        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("skuName",skuName);
        params.put("brandName",brandName);
        params.put("typeName",typeName);
        params.put("name",name);
        params.put("workOrderId",workOrderId);
        params.put("queryFlag","repair");

        Page<WcabUseInventoryProductVO> list = wcabUseInventoryProductService.pageListSku4app(pg, params);
        PageResult pageResult=new PageResult<>(list.getRecords(), list.getTotal());
        return pageResult;
    }

    @ApiOperation(value = "查询货道上产品库存(领用列表)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "productCategoryId", value = "产品分类id", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "productTypeId", value = "产品类型id", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "skuName", value = "SKU名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "brandName", value = "品牌名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "typeName", value = "产品类型名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCodes", value = "柜子编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/recycle/list")
    public PageResult<WcabUseInventoryProductVO> listRecycle(
            @RequestParam(name = "page",  defaultValue = "1", required = false) Integer page
            , @RequestParam(name = "limit",  defaultValue = "10", required = false) Integer limit
            , @RequestParam(name = "sort",  defaultValue = "create_time", required = false) String sort
            , @RequestParam(name = "order",  defaultValue = "asc", required = false) String order
            , Long productCategoryId
            , Long productTypeId
            , String cabCodes
            , String skuName
            , String brandName
            , String typeName
            , String name
            , Long workOrderId
    ) {
        Page<WcabUseInventoryProductVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("productCategoryId",productCategoryId);
        params.put("productTypeId",productTypeId);
        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("skuName",skuName);
        params.put("brandName",brandName);
        params.put("typeName",typeName);
        params.put("name",name);
        params.put("workOrderId",workOrderId);
        params.put("queryFlag","recycle");

        Page<WcabUseInventoryProductVO> list = wcabUseInventoryProductService.pageListSku4app(pg, params);
        PageResult pageResult=new PageResult<>(list.getRecords(), list.getTotal());
        return pageResult;
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long wcabUseInventoryProductId) {
        WcabUseInventoryProductVO wcabUseInventoryProductVO = wcabUseInventoryProductService.get4appById(wcabUseInventoryProductId);
        if (wcabUseInventoryProductVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), wcabUseInventoryProductVO);
    }

    @PostMapping()
    public JsonResult add(@Validated WcabUseInventoryProductForm wcabUseInventoryProductForm) {
        if (wcabUseInventoryProductService.add(wcabUseInventoryProductForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
